"""
Case Type   : 资源池化集群DMS reform优化
Case Name   : 踢出主机->reform在flush_copy阶段kill新主机
Create At   : 2024/05/13
Owner       : @kyleze
Description :
    1.踢出主机
    2.持续观察DMS日志
    3.reform在flush_copy阶段kill新主机
    4.拉起原主，查询集群状态
Expect      :
    1.成功
    2.reform进入flush_copy阶段
    3.成功
    4.集群恢复正常
History     :
"""

import os
import unittest
import time

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH

from yat.test import Node
from yat.test import macro


class DmsRform(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.dirname(__file__)} start----')
        self.com = Common()
        self.constant = Constant()
        self.primary_sh = CommonSH("PrimaryDbUser")
        self.pri_user_node = Node(node="PrimaryDbUser")
        self.st1_user_node = Node(node="Standby1DbUser")
        self.st2_user_node = Node(node="Standby2DbUser")
        self.dms_log_file = os.path.join('$GAUSSLOG', 'pg_log/DMS/run/dms.rlog')
        self.reform_start = int(self.pri_user_node.sh(
            f'source {macro.DB_ENV_PATH};grep "dms_reform_set_start" '
            f'{self.dms_log_file}|wc -l').result())
        self.flush_copy = int(self.pri_user_node.sh(
            f'source {macro.DB_ENV_PATH};'
            f'grep "dms_reform_flush_copy_parallel enter" '
            f'{self.dms_log_file}|wc -l').result())

    def test_dms(self):
        step = '--step1:踢出主机 expect:成功--'
        self.log.info(step)
        kick_res = self.primary_sh.exec_cm_ctl('stop', '-n 1')
        self.log.info(kick_res)
        self.assertTrue(kick_res, "执行失败" + step)

        step = '--step2:持续观察DMS日志 expect:reform进入flush_copy阶段--'
        self.log.info(step)
        reform_flush_num = 0
        new_primary = ''
        for i in range(10):
            reform_flush_num = int(self.st1_user_node.sh(
                f'grep "dms_reform_flush_copy_parallel enter" '
                f'{self.dms_log_file}|wc -l').result())
            if reform_flush_num > self.flush_copy:
                new_primary = self.st1_user_node
                break
            reform_flush_num = int(self.st2_user_node.sh(
                f'grep "dms_reform_flush_copy_parallel enter" '
                f'{self.dms_log_file}|wc -l').result())
            if reform_flush_num > self.flush_copy:
                new_primary = self.st2_user_node
                break
        self.assertTrue(reform_flush_num > self.flush_copy, "执行失败" + step)

        step = '--step3:reform在flush_copy阶段kill新主机 expect:成功--'
        self.log.info(step)
        kill_cmd = "ps ux | grep '\-D' | grep gaussdb | grep -v grep | " \
                   "awk '{print $2}' | xargs kill -9"
        st1_kill_res = new_primary.sh(kill_cmd).result()
        self.log.info(st1_kill_res)
        self.assertIn('', st1_kill_res, "执行失败" + step)

        step = '--step4:查看集群状态 expect:集群状态正常--'
        self.log.info(step)
        start_res = self.primary_sh.exec_cm_ctl('starp', '-n 1')
        self.log.info(start_res)
        self.assertTrue(start_res, "执行失败" + step)
        time.sleep(20)
        status = self.primary_sh.get_db_cluster_status('status')
        self.assertTrue("Normal" in status, "执行失败" + step)

    def tearDown(self):
        self.log.info('--还原环境--')
        switchover_res = self.primary_sh.exec_cm_ctl('switchover', '-a')
        self.log.info(switchover_res)
        self.assertTrue(switchover_res, "环境还原失败")
        self.log.info(f'----{os.path.dirname(__file__)} end----')
